Home:ALL Converter>The difference between "C c = new C()" and "A c = new C()" when C is a subclass of A in Java

The difference between "C c = new C()" and "A c = new C()" when C is a subclass of A in Java

Ask Time:2013-02-17T12:12:54         Author:prosseek

Json Formatter

Let's say we have class A as a parent class, and class C that extends it.

class A
{
    void m()
    {
        System.out.println("A.m");
    }
}

class C extends A
{
    @Override
    void m()
    {
        System.out.println("C.m");
    }
}

What's the difference between reference A a and C c when we use them to point to the same object type, for example A a = new C(); and C c = new C();?

From this question: Java inheritance vs. C# inheritance, it looks like that as a and c points to object type of C, and there seems no difference in using them.

I tested this code, and they all prints C.m.

class inherit {

    void x(A a)
    {
        a.m();
    }

    public static void main(String[] args) {
        System.out.println("hello");
        A a = new C();
        C c = new C();
        a.m();
        c.m();

        new inherit().x(a);
        new inherit().x(c);

    }
}

Author:prosseek,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/14917903/the-difference-between-c-c-new-c-and-a-c-new-c-when-c-is-a-subclass
Hui Zheng :

That depends what the object is going to be used for. \n\nIf what you actually need is an object that has A's interface(i.e. A's type), it's strongly recommended to use A a = new C();. This way it makes it clear that you want an A interface, not a C implementation. Later when you change your mind, you can safely change it to A a = new Another_Subtype_Of_A(); without breaking other code.\n\nThis is especially true when A is an interface(In your case, A is a class). For example, if you just want a list, List list = new ArrayList(); is clearly better than ArrayList list = new ArrayList();. That's called \"programming to interface, not implementation\".\n\nIf you're creating an object that specifically needs C's interfaces(esp. those not present in A), you'd better choose C c = new C();. If you write A a = new C() instead, sooner or later you still have to cast the object to C(because A doesn't have all of your desired interfaces), so why bother?",
2013-02-17T04:34:08
Dennis :

It's not about the runtime type of the variable. You may only know you have a Vehicle object at compile time and based on user input, that may be a GarbageTruck or SportsCar.\n\nGarbageTruck t;\n...\nt = new SportsCar(); //can't do this!\n\n// so we do this:\nVehicle t;\nif(user.isFast()) {\n t = new SportsCar();\n} else {\n t = new GarbageTruck();\n}\n",
2013-02-17T04:17:57
Dancrumb :

Java is all about Interfaces and Implementations.\n\nAn Interface is simply a set of public fields (methods & properties) the describe how users can interact with a class that implements the interface.\n\nAn Implementation is the code that actually makes those methods and properties do something. An Implementation can be a class that implements an interface, or it could be a subclass of some other implementation.\n\nWhen you instantiate a class, you're writing code like:\n\nInterface a = new Implementation();\n\n\nOften times, we wrap the Interface and the Implementation all together... put another way, when we define a class, whether we're explicitly implementing an interface or not, we're defining an Interface with every public method we write.\n\nThus, it's the Interface that affects what methods we can call, but it's the Implementation that affects what happens when we call them.",
2013-02-17T04:23:37
yy